home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 526-550 / disk_542 / chemnimate / cmtsource.lzh / k5.h < prev    next >
C/C++ Source or Header  |  1991-06-10  |  4KB  |  169 lines

  1. /****************************************************************
  2.  *                                *
  3.  *    This include was written to be able to use        *
  4.  *                                *
  5.  *        LINKED LISTS                    *
  6.  *                                *
  7.  *                                *
  8.  *==============================================================*
  9.  *    Author    Date    Comment                    *
  10.  *    Jos Horemans    From the book 'De C Taal', Jos Horemans *
  11.  *            (place:) Geel, Cipal Institue 1989    *
  12.  *    KvGend    06Jun91    typed it in, corrected a BIG BUG.    *
  13.  *            rewrote half of the routines, added:    *
  14.  *            -opruimen()  -> cleanup             *
  15.  *            -makeinfo()  -> create a INFO_T structure*
  16.  *            -gimmelinnr() ->return the address of the*
  17.  *                    INFO_T structure    *
  18.  *                                *
  19.  *                                *
  20.  ****************************************************************/
  21.  
  22. #define MALLOC(x)  ((x*) malloc(sizeof(x)))
  23. #define BOOLEAN int
  24. #define TRUE 1
  25. #define FALSE 0
  26.  
  27. struct info
  28. {
  29.   int linnr;
  30.   int token;
  31.   char *string;
  32. };
  33. typedef struct info INFO_T;
  34. struct node
  35. {
  36.   INFO_T *item;
  37.   struct node *next;
  38. };
  39. typedef struct node NODE_T;
  40. struct head
  41. {
  42.   int length;
  43.   NODE_T *first;
  44.   NODE_T *last;
  45. };
  46. typedef struct head HEAD_T;
  47.  
  48. /************** create the head of a linked list */
  49. HEAD_T *
  50. create ()
  51. {
  52.   HEAD_T *newh = NULL;
  53.   if (newh = MALLOC (HEAD_T))
  54.     {
  55.       newh->length = 0;
  56.       newh->first = newh->last = NULL;
  57.     }
  58.   return (newh);        /* if no mem exists, then return NULL */
  59. }
  60.  
  61. /*************** create a new node */
  62. NODE_T *
  63. inst_node (INFO_T * val, NODE_T * ptr)
  64. {
  65.   NODE_T *new = NULL;
  66.   if (new = MALLOC (NODE_T))
  67.     {
  68.       new->item = val;
  69.       new->next = ptr;
  70.     }
  71.   return (new);
  72. }
  73.  
  74. /********* insert an item at the end of a list */
  75. BOOLEAN
  76. append (INFO_T * data, HEAD_T * list)
  77. {
  78.   NODE_T *new = NULL;
  79.   if (new = inst_node (data, NULL))
  80.     {
  81.       if (list->length)
  82.     list->last->next = new;    /*set the 'next' of the previous one*/
  83.       else
  84.     list->first = new;
  85.       list->last = new;
  86.       list->length++;
  87.       return (TRUE);
  88.     }
  89.   return (FALSE);
  90. }
  91.  
  92. /*
  93.  *******************************makeinfo(int linnr,int tok, char *str)***
  94.  */
  95. INFO_T *
  96. makeinfo (int linnr, int tok, char *str)
  97. {
  98.   char *newstr = NULL;
  99.   INFO_T *newi = NULL;
  100.   int i;
  101.  
  102.   newi = MALLOC (INFO_T);    /* geheugen alloceren voor INFO_T */
  103.   newstr = (char *) malloc (strlen (str) + 1);    /* alloc mem for string  */
  104.   if (newi && newstr)
  105.     {
  106.       /* de INFO opzetten */
  107.       newi->linnr = linnr;
  108.       newi->token = tok;
  109.       newi->string = newstr;
  110.       /* en de string copieren */
  111.       for (i = 0; i < strlen (str); i++)
  112.     newstr[i] = str[i];
  113.       newstr[i] = '\0';
  114.     }
  115.   if (!newstr && newi)        /* no mem for newstr, but there was for newi */
  116.     {
  117.       free (newi);
  118.       newi = NULL;
  119.     }
  120.   return (newi);
  121. }
  122.  
  123.  
  124. /*
  125.  **************************************opruimen(HEAD_T koppie)***********
  126.  */
  127. BOOLEAN
  128. opruimen (HEAD_T * koppie)
  129. {
  130.   int i;
  131.   NODE_T *nod, *nod2;
  132. /* van voorafaan beginnen */
  133.   nod = koppie->first;
  134.   for (i = 0; nod != NULL; i++)
  135.     {
  136.       free (nod->item->string);    /* ruim string op */
  137.       free (nod->item);        /*ruim INFO_T op */
  138.       nod2 = nod->next;
  139.       free (nod);        /* ruim NODE_T op */
  140.       nod = nod2;
  141.     }
  142.   koppie->first = NULL;
  143.   koppie->last = NULL;
  144.   return (TRUE);        /* geslaagd ! */
  145. }
  146.  
  147. /*
  148.  ************************************gimmelinnr(HEAD_T *kop,linenr)*****
  149.  */
  150. INFO_T *
  151. gimmelinnr (HEAD_T * kop, linenr)
  152. {
  153.   NODE_T *nod;
  154.  
  155. /* start at the beginning */
  156.   nod = kop->first;
  157.  
  158. /* search whole stuff through */
  159.   while (nod->item->linnr != linenr && nod != NULL)
  160.     nod = nod->next;
  161.  
  162. /* got 'em ? */
  163.   if (nod->item->linnr == linenr)
  164.     return (nod->item);
  165.  
  166. /* OH NO , linenr was not in the list.*/
  167.   return (NULL);
  168. }
  169.